/**
 * HashCode组件
 */
export default class HashCodeComponent implements Chaos.IDisposable {
    private static instance: HashCodeComponent = new HashCodeComponent();
    /**
     * HashCode组件
     */
    public static get Default(): HashCodeComponent {
        return this.instance;
    }

    constructor() {
        window.addEventListener('unload', this.Dispose.bind(this));
    }

    Dispose() {
        window.removeEventListener('unload', this.Dispose);
    }

    public Compute(input: Object): number {
        const content = JSON.stringify(input);
        const length = content.length;

        let code = 0;
        let offset = 0;

        for (let index = 0; index < length; ++index) {
            code = 31 * code + content.charCodeAt(offset++);
        }

        return code;
    }
}